//index.js
//获取应用实例
const app = getApp()
const Pager = require('components/aotoo/core')

const data = ['https://hbimg.huabanimg.com/bac707919ff201dce07e8597a672047abdec4fed54523-9ddBTa_fw658/format/webp', 'https://hbimg.huabanimg.com/202528e390034058d2954d86e59a1a638c33acd527a45a-a9ZFFw_fw658/format/webp', 'https://hbimg.huabanimg.com/359bb092dfb943a69932180997306ef83ad8964428aea-vnyuPi_fw658/format/webp']

const data2 = ['https://hbimg.huabanimg.com/b5ae9f64944a45fa41461c8e752842f6046ac12d3ce5e-hBSZ3K_fw658/format/webp', 'https://hbimg.huabanimg.com/70d87d564f183eebac55508a788d374fddcc76e33d711-e1Fjyw_fw658/format/webp', 'https://hbimg.huabanimg.com/5789c52a6a0eb28c712b12c7a30cb785f428208ab543-rtzUnz_fw658/format/webp']

const data3 = ['https://hbimg.huabanimg.com/1ed164c8c43f7c303398c83b176de81617885dc74d1d8-RT72gz_fw658/format/webp', 'https://hbimg.huabanimg.com/48686783e002687ed5fe3c1ccae16d289b8ffbc3250b77-iWJ5RI_fw658/format/webp', 'https://hbimg.huabanimg.com/359bb092dfb943a69932180997306ef83ad8964428aea-vnyuPi_fw658/format/webp']


const img = (src, type) => {
  let content = []
  let dot = []
  src.map((item, ii) => {
    content.push({
      img: {
        src: item,
        mode: 'scaleToFill',
        longpress: 'onSavePic?src='+item,
        'lazy-load': true,
      },
      itemClass: 'item-pic-wrap'
    })
    if (type === 'dot') {
      dot.push({
        title: ' ',
        itemClass: ii === 0 ? 'active' : ''
      })
    }
  })
  return {content, dot}
}
const swiperData = img(data, 'dot')
const swiperData2 = img(data2, 'dot')
const swiperData3 = img(data3, 'dot')
const listAdaper = () => {
  return [
    {
      '@list': {
        data: swiperData.content,
        footer: {
          '@list': {
            $$id: 'swiper-dot-cu',
            data: swiperData.dot,
            listClass: 'ss-banner-dot',
            itemClass: 'item-dot'
          }
        },
        type: {
          is: 'swiper',
          interval: 2000,
          duration: 500,
          bindchange: 'bindchange'
        },
        listClass: 'ss-banner'
      }
    },
    {
      '@list': {
        data: swiperData2.content,
        footer: {
          '@list': {
            $$id: 'swiper-dot-cu2',
            data: swiperData2.dot,
            listClass: 'ss-banner-dot',
            itemClass: 'item-dot'
          }
        },
        type: {
          is: 'swiper',
          interval: 2000,
          duration: 500,
          bindchange: 'bindchange?id=swiper-dot-cu2',
          'next-margin': '75rpx',
          // 'snap-to-edge': true
          circular: true
        },
        listClass: 'ss-banner primary'
      }
    },
    {
      '@list': {
        data: swiperData3.content,
        footer: {
          title: [
            {
              $$id: 'swiper-dot-num',
              title: '1'
            },
            {
              title: '/'+swiperData3.content.length
            }
          ],
          itemClass: 'ss-banner-num'
        },
        type: {
          is: 'swiper',
          interval: 2000,
          duration: 500,
          bindchange: 'bindchange?type=num'
        },
        listClass: 'ss-banner'
      }
    }
  ]
}
Pager({
  data: {
    pages: Pager.list({
      data: listAdaper(),
      listClass: 'demo',
      itemClass: 'demo-item',
      type: {
        is: 'swiper',
        'indicator-dots': true,
        vertical: true,
        interval: 2000,
        duration: 500
      },
      footer:{
        title: ' ',
        itemClass: 'icon-goup size-title-lg ss-center color-grey xx'
      }
    })
  },
  bindchange(e, params, inst) {
    let _type = params.type
    let _id = params.id
    let _current = e.detail.current
    if (!_type) {
      const $swiperDot = this.getElementsById(_id || 'swiper-dot-cu')
      const _newDot = $swiperDot.getData().data
      _newDot.map((item, ii) => {
        item.itemClass = ''
        if (ii === _current) {
          item.itemClass +=' active'
        }
        return item
      })
      $swiperDot.update(_newDot)
    }
    else {
      const $swiperDotNum = this.getElementsById('swiper-dot-num')
      $swiperDotNum.update({title: parseInt(_current + 1)})
    }
  },
  onShow: function () {},
  onReady: function () {},
  onLoad: function () {},
   // onSavePic (e, param) {
  //   let _this = this
  //   var url = param.src;
  //   wx.showActionSheet({
  //       itemList: ['保存到相册'],
  //       success(res) {
  //           wx.getSetting({
  //               success: (res) => {
  //                   if (!res.authSetting['scope.writePhotosAlbum']) {
  //                       wx.authorize({
  //                           scope: 'scope.writePhotosAlbum',
  //                           success: () => {
  //                               // 同意授权
  //                               this.saveImgInner(url);
  //                           },
  //                           fail: (res) => {
  //                               console.log(res);
  //                               wx.showModal({
  //                                   title: '保存失败',
  //                                   content: '请开启访问手机相册权限',
  //                                   success(res) {
  //                                       wx.openSetting()
  //                                   }
  //                               })
  //                           }
  //                       })
  //                   } else {
  //                       // 已经授权了
  //                       _this.saveImgInner(url);
  //                   }
  //               },
  //               fail: (res) => {
  //                   console.log(res);
  //               }
  //           })   
  //       },
  //       fail(res) {
  //           console.log(res.errMsg)
  //       }
  //   })
  // },
  // // 长按保存功能--保存部分
  // saveImgInner (url) {
  //   console.log('url', url);
    
  //   wx.getImageInfo({
  //       src: url,
  //       success: (res) => {
  //           let path = res.path;
  //           wx.saveImageToPhotosAlbum({
  //               filePath: path,
  //               success: (res) => {
  //                   console.log(res);
  //                   wx.showToast({
  //                       title: '已保存到相册',
  //                   })
  //               },
  //               fail: (res) => {
  //                   console.log(res);

  //               }
  //           })
  //       },
  //       fail: (res) => {
  //           console.log(res);
  //       }
  //   })
  // },
  onSavePic: function(e, param) {
    // const $modal = this.getElementsById('modal')
    var url = param.src;
    console.log('url', url);
    let vm = this
    wx.downloadFile({
      url: url,
      success (res) {
        // 只要服务器有响应数据，就会把响应内容写入文件并进入 success 回调，业务需要自行判断是否下载到了想要的内容
        if (res.statusCode === 200) {
          wx.saveImageToPhotosAlbum({
            filePath: res.tempFilePath,
            success(res) { 
              wx.showToast({
                  title: '图片保存成功',
                  icon: 'none',
                  duration: 3000
              })
            },
            fail(res){
              console.log('fail', res);
            }
          })
        }
      },
      fail(res){
        wx.showToast({
            title: '图片保存失败',
            icon: 'none',
            duration: 3000
        })
      }
    })
  },
})
